package router

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"live-core/core"
	"live-core/logging"
)

func Streams(c *gin.Context) {
	c.IndentedJSON(200, Message{Status: 1, Payload: core.StreamManager.StreamsList()})
}

func StreamsMultiControlAdd(c *gin.Context) {
	var payload core.StorageST
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("call", "BindJSON"))
		return
	}
	if payload.Streams == nil || len(payload.Streams) < 1 {
		c.IndentedJSON(400, Message{Status: 0, Payload: core.ErrorStreamsLen0.Error()})
		logging.ZapLog.Error(core.ErrorStreamsLen0.Error(), zap.String("call", "len(payload)"))
		return
	}
	var resp = make(map[string]Message)
	var FoundError bool
	for k, v := range payload.Streams {
		err = core.StreamManager.StreamAdd(k, v)
		if err != nil {
			logging.ZapLog.Error(err.Error(), zap.String("stream", k), zap.String("call", "StreamAdd"))
			resp[k] = Message{Status: 0, Payload: err.Error()}
			FoundError = true
		} else {
			resp[k] = Message{Status: 1, Payload: core.Success}
		}
	}
	if FoundError {
		c.IndentedJSON(200, Message{Status: 0, Payload: resp})
	} else {
		c.IndentedJSON(200, Message{Status: 1, Payload: resp})
	}
}

func StreamsMultiControlDelete(c *gin.Context) {
	var payload []string
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("call", "BindJSON"))
		return
	}
	if len(payload) < 1 {
		c.IndentedJSON(400, Message{Status: 0, Payload: core.ErrorStreamsLen0.Error()})
		logging.ZapLog.Error(core.ErrorStreamsLen0.Error(), zap.String("call", "len(payload)"))
		return
	}
	var resp = make(map[string]Message)
	var FoundError bool
	for _, key := range payload {
		err := core.StreamManager.StreamDelete(key)
		if err != nil {
			logging.ZapLog.Error(err.Error(), zap.String("stream", key), zap.String("call", "StreamDelete"))
			resp[key] = Message{Status: 0, Payload: err.Error()}
			FoundError = true
		} else {
			resp[key] = Message{Status: 1, Payload: core.Success}
		}
	}
	if FoundError {
		c.IndentedJSON(200, Message{Status: 0, Payload: resp})
	} else {
		c.IndentedJSON(200, Message{Status: 1, Payload: resp})
	}
}

func StreamAdd(c *gin.Context) {
	var payload core.StreamST
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "BindJSON"))
		return
	}
	err = core.StreamManager.StreamAdd(c.Param("uuid"), payload)
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "StreamAdd"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamEdit(c *gin.Context) {
	var payload core.StreamST
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "BindJSON"))
		return
	}
	err = core.StreamManager.StreamEdit(c.Param("uuid"), payload)
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "StreamEdit"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamDelete(c *gin.Context) {
	err := core.StreamManager.StreamDelete(c.Param("uuid"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "StreamDelete"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamReload(c *gin.Context) {
	err := core.StreamManager.StreamReload(c.Param("uuid"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "StreamReload"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamInfo(c *gin.Context) {
	info, err := core.StreamManager.StreamInfo(c.Param("uuid"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")), zap.String("call", "StreamInfo"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: info})
}
